import 'package:cloud_medicine/components/user_avatar.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/chat_api.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/model/search_chat.dart';
import 'package:flutter/material.dart';

class SearchChatPage extends StatefulWidget {
  @override
  _SearchChatPageState createState() => _SearchChatPageState();
}

class _SearchChatPageState extends State<SearchChatPage> {
  final TextEditingController controller = TextEditingController();
  bool _loading = false;
  List<SearchChatItem> _chatList = [];

  Widget buildTextField() {
    //theme设置局部主题
    return TextField(
      onChanged: (value) {
        _loadData(value);
      },
      controller: controller,
      cursorColor: Colors.black,
      //设置光标
      decoration: InputDecoration(
          border: InputBorder.none,
          icon: Icon(Icons.search),
          hintText: "搜索",
          hintStyle: new TextStyle(fontSize: 16, color: Colors.grey)),
      style: new TextStyle(fontSize: 16, color: Colors.black),
    );
  }

  // 加载数据
  Future<Null> _loadData(String value) async {
    if(mounted) {
      setState(() {
        _chatList.clear();
      });
    }
    if(value == '') {
      return;
    }
    setState(() {
      _loading = true;
    });
    dynamic res = await searchChat_api('$backIP/chat/search', {
      'content': value,
    });
    if (res['code'] == 0) {
      res['data'].forEach((dynamic chatItem) {
        if (mounted) {
          setState(() {
            SearchChatItem chatItemModel = SearchChatItem.init(chatItem);
            _chatList.add(chatItemModel);
          });
        }
      });
    } else {
      errorToast(res['message']);
    }
    setState(() {
      _loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Container(
          decoration: new BoxDecoration(
            border: Border.all(color: Colors.grey[200], width: 1.0), //灰色的一层边框
            color: Colors.grey[200],
            borderRadius: new BorderRadius.all(new Radius.circular(15.0)),
          ),
          alignment: Alignment.center,
          height: 36,
          padding: EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0),
          child: buildTextField(),
        ),
        actions: <Widget>[
          new Center(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: GestureDetector(
                child: Text("取消"),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
            ),
          )
        ],
      ),
      body: _loading
          ? loadingWidget(_loading)
          : ListView.builder(
          itemCount: _chatList.length,
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                // TODO: 进入聊天具体信息页面
              },
              child: Container(
                height: 60,
                margin: EdgeInsets.all(10),
                child: Row(
                  children: <Widget>[
                    UserAvatar(
                      avatar: _chatList[index].authorBasicInfo.avatar,
                      imageStyle: 60,
                    ),
                    SizedBox(width: 10),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        SizedBox(height: 3),
                        Text(_chatList[index].authorBasicInfo.realname),
                        RichText(
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          text: TextSpan(
                            text: '${_chatList[index].recordNum}条与“',
                            style: TextStyle(
                                color: Colors.black, fontSize: 12.0),
                            children: <TextSpan>[
                              TextSpan(
                                  text: controller.text,
                                  style: TextStyle(
                                      color: Colors.lightBlueAccent)),
                              TextSpan(text: '”相关记录')
                            ],
                          ),
                        ),
                        SizedBox(height: 3),
                      ],
                    )
                  ],
                ),
              ),
            );
          }),
    );
  }
}
